package com.yuanduan.mplus.view;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.cj.framework.app.BaseActivity;
import com.cj.framework.util.HttpImp;
import com.cj.framework.util.IHttp;
import com.yuanduan.mplus.R;
import com.yuanduan.mplus.config.Config;
import com.yuanduan.mplus.presenter.MSpinnerAdapter;

import java.util.HashMap;

/**
 * 点评界面
 */
public class CommentsActivity extends BaseActivity implements  View.OnClickListener, AdapterView.OnItemSelectedListener {
    private Toolbar toolbar;
    private Spinner spinner;
    private Button btn_sendComments;
    private EditText et_content;
    private LinearLayout ll_img;
    private TextView tv_choosePerson;
    private CheckBox cb_zan;
    private ListView lv;
    private String[] names;
    private boolean isDianzan=false;
    private String content;
    //还有点评对象的数据;

    private Bitmap bitmap;
    //还有要通知的人的数据;


    private Dialog dialog;
    private IHttp iHttp=new HttpImp();
    private ProgressDialog progressDialog;
    private IHttp.HttpInfoCallbackWithIndex callback=new IHttp.HttpInfoCallbackWithIndex() {
        @Override
        public void onSucceed(Object result, int index) {
            switch (index){
                case 0:
                    progressDialog.dismiss();
                    //设置spinner
                    names= new String[]{"王小丫", "王小二", "李小三", "陈小四"};
                    ArrayAdapter<String> adapter=new ArrayAdapter<String>(CommentsActivity.this,android.R.layout.simple_spinner_dropdown_item,names);
                    spinner.setAdapter(adapter);
                    lv.setAdapter(new MAdapter());
                    break;
            }
        }

        @Override
        public void onFailed(Object result, int index) {

        }
    };
    @Override
    protected void findId() {
        toolbar= (Toolbar) findViewById(R.id.toolbar);
        spinner= (Spinner) findViewById(R.id.comments_spinner);
        btn_sendComments= (Button) findViewById(R.id.comments_btn);
        et_content= (EditText) findViewById(R.id.comments_et_content);
        ll_img= (LinearLayout) findViewById(R.id.comments_ll_pushImg);
        tv_choosePerson= (TextView) findViewById(R.id.comments_tv_pushPerson);
        cb_zan= (CheckBox) findViewById(R.id.comments_cb);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void logic() {
        toolbar.setTitle("点评");
        toolbar.setNavigationIcon(R.mipmap.back_arrow_black);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommentsActivity.this.finish();
            }
        });

        dialog=new Dialog(this);
        dialog.setTitle("选择通知人员");
        dialog.setContentView(R.layout.content_comment_dialog);
        ImageButton ib= (ImageButton) dialog.findViewById(R.id.comments_ib_close);
        ib.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        lv= (ListView) dialog.findViewById(R.id.comments_listview);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d("xx","clicked"+position);
            }
        });

        progressDialog=new ProgressDialog(this);
        spinner.setOnItemSelectedListener(this);
        spinner.setDropDownVerticalOffset(100);
        spinner.setDropDownWidth(300);
        //假设数据
        progressDialog.show();
        iHttp.httpInfoPostWithIndex(Config.URL,new HashMap<String, String>(),callback,0);


        ll_img.setOnClickListener(this);
        tv_choosePerson.setOnClickListener(this);
        btn_sendComments.setOnClickListener(this);
        cb_zan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    isDianzan=true;
                }else{
                    isDianzan=false;
                }
            }
        });
        et_content.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(!hasFocus){
                    content=et_content.getText().toString().trim();
                }
            }
        });
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_comments;
    }


    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.comments_spinner:
//                progressDialog.show();
//                iHttp.httpInfoPostWithIndex(Config.URL,new HashMap<String, String>(),callback,0);
                break;
            case R.id.comments_ll_pushImg:
                //添加图片附件
                break;
            case R.id.comments_tv_pushPerson:
//                选择要通知的人
                dialog.show();
                break;
            case R.id.comments_btn:
                if(!checkAll()){
                    return;
                }
                iHttp.httpInfoPostWithIndex(Config.URL,new HashMap<String, String>(),callback,1);
                break;
        }
    }

    private boolean checkAll() {
       if(TextUtils.isEmpty(content)){
           return false;
       }
       return true;
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        //获取点评对象信息
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    private class MAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return names.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view= LayoutInflater.from(CommentsActivity.this).inflate(R.layout.comments_listview_notifyperson,null);
            TextView tv= (TextView) view.findViewById(R.id.comments_tv_item);
            tv.setText(names[position]);
            return view;
        }
    }
}
